package com.zk.algorithm.array;

import com.zk.algorithm.annotation.Easy;
import com.zk.algorithm.annotation.Finished;
import com.zk.algorithm.annotation.LeetCodeExplore;

import java.util.HashSet;
import java.util.Set;

/**
 * @author zk
 */
@Easy
@LeetCodeExplore
@Finished(false)
public class ValidSudoku {

    // 每一行
    // 每一列
    // 每个 3 * 3
    public boolean isValidSudoku(char[][] board) {
        // Every row
        for (int i = 0; i < 9; i++) {
            Set<Character> rowSet = new HashSet<>(9);
            for (int j = 0; j < 9; j++) {
                if (board[i][j] != '.' && !rowSet.add(board[i][j])) {
                    return false;
                }
            }
        }

        // Every Column
        for (int i = 0; i < 9; i++) {
            Set<Character> columnSet = new HashSet<>(9);
            for (int j = 0; j < 9; j++) {
                if (board[j][i] != '.' && !columnSet.add(board[j][i])) {
                    return false;
                }
            }
        }


        // Every 3 * 3 box
        Set<Character> boxSet = new HashSet<>(9);
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j] != '.' && !boxSet.add(board[i][j])) {
                    return false;
                }
            }
        }

        boxSet.clear();
        for (int i = 3; i < 6; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j] != '.' && !boxSet.add(board[i][j])) {
                    return false;
                }
            }
        }

        boxSet.clear();
        for (int i = 6; i < 9; i++) {
            for (int j = 0; j < 3; j++) {
                if (board[i][j] != '.' && !boxSet.add(board[i][j])) {
                    return false;
                }
            }
        }

        return true;
    }

}
